//
//  PasswordResetVC.m
//  EPTool
//
//  Created by guoshiqing on 2018/4/25.
//  Copyright © 2018年 云数动能科技. All rights reserved.
//

#import "PasswordResetVC.h"
#import "PasswordResetCell.h"
#import "Macro.h"
#import "BottomToolBar.h"
#import "AccountSysVM.h"
#import "NSString+Regex.h"
#import "NotificationHelp.h"
#import "ToastView.h"
#import "ErrorCode.h"
#import "UserSettings.h"

#define PwdResetOld     @"旧密码"
#define PwdResetNew     @"新密码"

#define PwdResetOld_Placeholder     @"请输入旧密码"
#define PwdResetNew_Placeholder     @"请输入新密码"

@interface PasswordResetVC ()
@property (nonatomic, strong) BottomToolBar *barBtn;

@property (nonatomic, copy) NSString *oldPwd;
@property (nonatomic, copy) NSString *nowPwd;
@end

@implementation PasswordResetVC

- (void)viewDidLoad {

    self.title = @"修改密码";
    self.textList = @[@[PwdResetOld, PwdResetNew]];
    self.detailTextList = @[@[PwdResetOld_Placeholder, PwdResetNew_Placeholder]];
    self.tableView.bounces = NO;
    self.tableView.scrollEnabled = NO;
    [super viewDidLoad];
    [self.tableView addSubview:self.barBtn];
    self.barBtn.bottom = self.tableView.height - TabBar_Height;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)];
    tap.cancelsTouchesInView = NO;
    [self.view addGestureRecognizer:tap];
}

-(void)viewTapped:(UITapGestureRecognizer *)tap
{
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 接口调用返回结果处理
- (void)handelRequestData:(BaseJSONEntity *)returnValue
{
    [self hideProcessingHud];
    if (0 != returnValue.ret) {
        
        // 三次密码错误: 提示是否修改密码
        if (C_Login_3TimesError == returnValue.ret) {
            [self showAlertWithMessage:@"" title:returnValue.msg cancelStr:@"否" sureStr:@"是" cancel:^(UIAlertAction * _Nonnull action) {
            } sure:^(UIAlertAction * _Nonnull action) {
                [NotificationHelp postNotification:NotificationGo2LoginVC object:nil];
            }];
            return;
        }
        // 六次密码错误，直接跳出
        else if (C_Login_6TimesError == returnValue.ret) {
            [NotificationHelp postNotification:NotificationGo2LoginVC object:nil];
            return;
        }
        [self showAlertWithTitle:returnValue.msg];
        return;
    }
    
    if ([returnValue.data isKindOfClass:[NSDictionary class]]) {
        UserSaveInfo.token = [returnValue.data objectForKey:@"d-tk"];
    }
    
    [self dismissVC];
}

#pragma mark - 懒加载
- (BottomToolBar *)barBtn
{
    if (!_barBtn) {
        _barBtn = [[BottomToolBar alloc] initWithFrame:CGRectMake(Cell_X, 0, Cell_W, Cell_H) andBtnNames:@[@"完成"] andTextColors:[NSMutableArray arrayWithObjects:[UIColor whiteColor], nil] andBkColors:[NSMutableArray arrayWithObjects:[EPColor epColor_Blue], nil] andMaxCount:1];
        
        WEAKSELF
        _barBtn.tapToolBarBlock = ^(NSUInteger btnIndex, NSString *btnName) {
            if ([weakSelf checkPassword:weakSelf.oldPwd] && [weakSelf checkPassword:weakSelf.nowPwd])
            {
                [weakSelf sendCheckPwdRequest];
            }
        };
    }
    return _barBtn;
}

#pragma mark - 发送密码验证请求
- (void)sendCheckPwdRequest
{
    [self showProcessingHud:@"..."];
    [(AccountSysVM *)self.baseVM checkPwdForResetPwdWithOldPwd:self.oldPwd andNewPwd:self.nowPwd];
}

#pragma mark - Table view data source
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return Cell_H;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return Cell_H;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, Cell_H)];
    view.backgroundColor = [UIColor clearColor];
    
    UILabel *lblHead = [[UILabel alloc] initWithFrame:CGRectMake(Cell_X, 22.f, Cell_W, 17.f)];
    lblHead.text = [NSString stringWithFormat:@"%@%@", @"您的登录账号是：", UserSaveInfo.mobile];
    lblHead.font = SystemFont(12.f);
    lblHead.textColor = [EPColor epColor_descMessageGray];
    [view addSubview:lblHead];
    return view;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, Cell_H)];
    view.backgroundColor = [UIColor clearColor];
    
    UILabel *lblHead = [[UILabel alloc] initWithFrame:CGRectMake(Cell_X, 5.f, Cell_W, 17.f)];
    lblHead.text = @"密码规则";
    lblHead.font = SystemFont(12.f);
    lblHead.textColor = [EPColor epColor_descMessageGray];
    [view addSubview:lblHead];
    return view;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    PasswordResetCell *cell = [[PasswordResetCell alloc] initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, Cell_H)];
    NSArray *subTextList = [self.textList objectAtIndex:indexPath.section];
    cell.textLabel.textColor = [EPColor epColor_TitleBlack];
    if ([subTextList isKindOfClass:[NSArray class]]) {
        NSString *subTitle = [subTextList objectAtIndex:indexPath.row];
        if ([subTitle isEqualToString:PwdResetNew]) cell.type = PasswordShowType_Plaintext;
        cell.strTitle = subTitle;
        cell.tag = indexPath.row;
        if (indexPath.row + 1 != subTextList.count) {
            cell.bottomLineLeftEdge = Cell_X;
        }
    }
    else
    {
        cell.strTitle = [self.textList objectAtIndex:indexPath.section];
    }
    
    NSArray *subPlacehoder = [self.detailTextList objectAtIndex:indexPath.section];
    if ([subPlacehoder isKindOfClass:[NSArray class]]) {
        cell.strPlaceholder = [subPlacehoder objectAtIndex:indexPath.row];
    }
    else
    {
        cell.strPlaceholder = [self.detailTextList objectAtIndex:indexPath.section];
    }
    
    WEAKSELF
    cell.txfBecomeBlock = ^(NSUInteger tag, NSString *string) {
        [weakSelf savePasswordWithTag:tag andPwd:string];
    };
    
    return cell;
}

#pragma mark - 对返回的密码进行处理
- (void)savePasswordWithTag:(NSUInteger)tag andPwd:(NSString *)string
{
    if (0 == tag) {
        self.oldPwd = string;
    }
    else
    {
        self.nowPwd = string;
    }
}

#pragma mark - 检查密码格式的合法性
- (BOOL)checkPassword:(NSString *)password
{
    NSString *tempString = [password deleteSpace];
    if (0 == tempString.length) {
        [ToastView showMsg:@"密码格式输入错误" inView:self.view];
        return NO;
    }
    return YES;
}

@end
